<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/26 0026
 * Time: 上午 9:04
 */
namespace app\admin\controller;

use think\Db;

class  agents extends  Permissions
{
    public function  index()
    {

        $post = $this->request->param();
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['phone'] = ['like', '%' . $post['keywords'] . '%'];
        }
        if(isset($post['create_time']) and !empty($post['create_time'])) {
            $min_time = strtotime($post['create_time']);
            $max_time = $min_time + 24 * 60 * 60;
            $where['add_time'] = [['>=',$min_time],['<=',$max_time]];
        }
       
        $model = Db::name('agent');

        $agent = empty($where) ? Db::name('agent')->alias('a')->join('ratio b','a.id=b.agent_id')
                 ->field('a.id,name,phone,grade,charge_ratio,last_money,status,add_time,update_time,request')->where('pid',0)->order('add_time desc')
                 ->paginate(20) : Db::name('agent')->alias('a')->join('ratio b','a.id=b.agent_id')
                 ->field('a.id,name,phone,grade,charge_ratio,last_money,status,add_time,update_time,request')->where($where)->where('pid',0)->order('add_time desc')
                 ->paginate(20,false,['query'=>$this->request->param()]);

            $this->assign('agents', $agent);
            $info['agent'] = Db::name('agent')->where('name')->find();
            $this->assign('info', $info);
            return $this->fetch();
    }


     /**
     * 管理员删除
     * @return [type] [description]
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $id     = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            $res    = Db::name('agent')->where('id',$id)->delete();
           
            
            if($res){
                if(false == Db::name('ratio')->where('agent_id',$id)->delete()) {
                return $this->error('删除失败');
                }else{
                return $this->success('删除成功','admin/agents/index');
              }
            }else{
                  return $this->error('删除失败'); 
            }

        }
    }
}